
#####################################################################################
# Author: Gaoqiu
# Date:   2022-01-20
#####################################################################################

BUILD_DIR := ./BUILD

# Select project -------------------------------------------------------------------#
PROJ_NAME := ble_controller
#PROJ_NAME := ble_multi_conn
#PROJ_NAME := ble_peripheral
#PROJ_NAME := ble_central

# Project Config -------------------------------------------------------------------#
PLATFORM := tlsr825x
CSTARTUP_PATH := platform/$(PLATFORM)/startup/cstartup_8258_new.S

# Linker Scripts path 
LINKER_PATH := ./boot.link

# Target  -------------------------------------------------------------------------#
TARGET_EXEC := ${BUILD_DIR}/${PROJ_NAME}.bin 
LIB_BLE     := ${BUILD_DIR}/liblt_8258_d.a

ELF         := ${BUILD_DIR}/${PROJ_NAME}.elf
LST         := ${BUILD_DIR}/${PROJ_NAME}.lst 
SIZE_DUMMY  := ${BUILD_DIR}/${PROJ_NAME}.txt 


# Find all C files in the corresponding directory------------------------------------#
CSRC := \
	$(wildcard board/*.c) \
	$(wildcard bsp/*.c) \
	$(wildcard component/usb/*.c) \
	$(wildcard component/kv/*.c) \
	$(wildcard hci_transport/*.c) \
	$(wildcard libc/*.c) \
	$(wildcard platform/${PLATFORM}/*.c) \
	$(wildcard profile/*.c) \
	$(wildcard stack/controller/*.c) \
	$(wildcard stack/crypt/*.c) \
	$(wildcard stack/host/*.c) \
	$(wildcard utils/*.c) \
	$(wildcard project/${PROJ_NAME}/*.c)

# Generate the source file for lib.	   
CSRC_LIB := \
	$(wildcard component/usb/*.c) \
	$(wildcard hci_transport/hci_h5.c)   \
	$(wildcard hci_transport/hci_slip.c) \
	$(wildcard hci_transport/hci_tr_h5.c) \
	$(wildcard hci_transport/hci_tr_h4.c) \
	$(wildcard stack/controller/*.c) \
	$(wildcard stack/host/*.c) \
	$(wildcard stack/crypt/*.c)  \
	$(wildcard utils/crc.c) \
	$(wildcard utils/lg_mem.c) \
	$(wildcard utils/math.c)     \
	$(wildcard utils/mdc.c)      \
	$(wildcard utils/mem_pool.c) \
	$(wildcard utils/msg.c)      \
	$(wildcard utils/pool_buf.c) \
	$(wildcard utils/queue.c) \
	$(wildcard utils/ring_buf.c) \
	$(wildcard utils/stimer.c) 

# Find all ASM files in the corresponding directory      
ASMSRC := $(wildcard *.S) $(wildcard $(CSTARTUP_PATH))  

INC_DIRS := . ./utils/ \
	./stack/ \
	./stack/controller/ \
	./stack/host/        \
	./stack/crypt/ \
	./hci_transport/ \
	./platform/ \
	./platform/tlsr825x/\
	./component/usb/ \
	./component/kv/ \
	./libc/ \
	./board/ \
	./bsp/ \
	./project/${PROJ_NAME}/
	
INC_FLAGS := $(addprefix -I,$(INC_DIRS))	

# Include all library files
# LIBS = $(wildcard lib_ble/liblt_8258_d.a)   
LIBS = $(wildcard lib_ble/liblt_8258_r.a)   
   
# Change the suffix of all C files and CC and .S files to .o to get a list of .o files
OBJS := $(CSRC:%.c=$(BUILD_DIR)/%.o) $(ASMSRC:%.S=$(BUILD_DIR)/%.o) 
OBJS_LIB := $(CSRC_LIB:%.c=$(BUILD_DIR)/%.o)

DEPS := $(OBJS:.o=.d)
DEPS_LIB := $(OBJS:.o=.d)

# which is compiler-------------------------------------------------------------------------#
CROSS_COMPILE := tc32-elf-

CC  := ${CROSS_COMPILE}gcc
CXX := ${CROSS_COMPILE}g++
LD  := ${CROSS_COMPILE}ld
AR  := ${CROSS_COMPILE}ar

# For Embedded
OBJCOPY := tc32-elf-objcopy -O binary
OBJDUMP := tc32-elf-objdump -x -D -l -S
OBJSIZE := tc32-elf-size  -t

# Options for release
CFLAGS  := -O2 -Wall -fshort-enums -ffunction-sections -fdata-sections -std=gnu99 -funsigned-bitfields \
           -fshort-wchar -fms-extensions -fsigned-char\
           -DTLSR825X -DMCU_STARTUP_8258

CPPFLAGS := $(INC_FLAGS) -MMD

# Options for linker
LDFLAGS := --gc-sections -T $(LINKER_PATH) 


#********************************************************************************
# Make Start
#********************************************************************************
.PHONY: build lib clean clean_obj help 
	

# Script Start
# build: print $(TARGET_EXEC) $(LST) $(SIZE_DUMMY) 
# lib: print $(LIB_BLE)
all: $(ELF) $(LST) show.options

# $(LIB_BLE): $(OBJS_LIB)
# 	@echo " "
# 	@echo "+++ Build Library Start:"
# 	@$(AR) -cr $@ $(OBJS_LIB)
# 	@echo " "
# 	@echo $(LIB_BLE)
# 	@echo " "
	
$(ELF) $(LST): $(OBJS) $(LIBS) 
	@echo " "
	@echo "+++ Link Start: $@"
	@$(LD) $(LDFLAGS) -o $(ELF) $(OBJS) $(LIBS) 
	@$(OBJCOPY) $(ELF)  $(ELF:.elf=.bin)	
	@$(OBJDUMP) $(ELF) >$(ELF:.elf=.lst)	
	@$(OBJSIZE) $(ELF)
	@echo " "

# Include the .d makefiles. The - at the front suppresses the errors of missing
# Makefiles. Initially, all the .d files will be missing, and we don't want those
# errors to show up.
-include $(DEPS)

$(BUILD_DIR)/%.o:%.c 
	@echo "+++ Building: $(<F)"
	@mkdir -p $(dir $@)
	@$(CC) $(CPPFLAGS) $(CFLAGS)  -MMD -c -o $@ $<
	
$(BUILD_DIR)/%.o:%.S
	@echo "+++ Building: $<"
	@mkdir -p $(dir $@)
	@$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
	
clean:
	@-rm -r ${BUILD_DIR}
	@echo " Clean done ..."
	
clean_lib:
	@-rm -f ${BUILD_DIR}/*.a 
	@echo " Clean LIB file done ..."
	
show.options:
    @echo "+++ Building: "
	@echo "    LD_FLAGS      = $(LDFLAGS)"	

help:
	@:
	${info Help}
	${info -------------------------------------------------------}
	${info make help                          - this help text}
	${info make build                         - compile binary}
	${info make lib                           - assemble library}
	${info make clean                         - clean}
	${info make clean_lib                     - clean lib files}
	${info -------------------------------------------------------}


   